package com.atguigu.gmall.order.rpc.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.bo.UserIdBo;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/inner/order")
public class OrderRpcController {


    @Autowired
    OrderInfoService orderInfoService;

    @GetMapping("/info/{orderId}")
    public Result<OrderInfo> getOrderInfo(@PathVariable("orderId") Long orderId){
        UserIdBo userIdBo = AuthContextHolder.getUserIdBo();
        QueryWrapper<OrderInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userIdBo.getUserId());
        wrapper.eq("id",orderId);
        //查询订单信息;  0-1
        OrderInfo info = orderInfoService.getOne(wrapper);
        return Result.ok(info);
    }

    @GetMapping("/gen/tradeno")
    public Result<String> generateOrderTradeNo(){
        UserIdBo idBo = AuthContextHolder.getUserIdBo();
        //生成一个订单的流水追踪号
        String tradeNo = orderInfoService.generateOrderTradeNo(idBo.getUserId());


        return Result.ok(tradeNo);
    }
}
